Transfer functions and time response

Here we'll define several transfer functions and investigate their responses to a variety of inputs.

Consider the transfer function below which defines our system

$$ G(s) = \frac{2s +25}{s^2 + 4s + 25} $$

This second order transfer function defines our system, which we can also define within Python.

This example will use the following:


In [2]:
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

Now we can define the transfer function using scipy

This system representation allows us to use many of the functions within scipy.signal.

There are many equivalent variations to define a continuous transfer function.

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.TransferFunction.html#scipy.signal.TransferFunction


In [3]:
num = [2, 25]
den = [1, 4, 25]

sys = signal.TransferFunction(num, den)

Step response

Recall the step response of our system is defined as

$$ Y(s) = U(s) G(s) $$

where $ U(s) $ is the unit step.

Here we use Scipy to numerically output the response


In [4]:
time, response = signal.step(sys)

Once we have the response we can plot it using matplotlib


In [5]:
plt.plot(time,response,label="Simulation")
plt.show()


Simulating any arbitrary input

We can use some built in functions for the common step and impulse inputs. However, if we want to apply any signal we need to use something different.

Here we'll find the response to our system given a ramp input

$$ u(t) = t $$

We need to define a time vector and an input vector


In [9]:
t = np.linspace(0, 5)
u = 1 * t
tout, resp, x = signal.lsim(sys, u, t)

Now we'll plot the output and input and make our plot a little more fancy


In [11]:
plt.plot(t, resp, label='Output')
plt.plot(t, u, label='Input')
plt.legend()
plt.title('Ramp Response')
plt.xlabel('Time (sec)')
plt.ylabel('Response')
plt.grid()
plt.show()